package com.qdairlines.controller.base;

import com.qdairlines.common.CustomException;
import com.qdairlines.common.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;



@Controller
public class BaseController {

	private Logger logger = LoggerFactory.getLogger(BaseController.class);

	/**
	 * 未知异常处理
	 * @param runtimeException
	 * @return
     */
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public Response runtimeExceptionHandler(Exception runtimeException) {
		StringWriter sw=new StringWriter();
		runtimeException.printStackTrace(new PrintWriter(sw,true));
		logger.error("未知异常："+sw.toString());//记录详细错误日志
		runtimeException.printStackTrace();//打印详细错误日志
		Response response = new Response();
		if(runtimeException instanceof BindException){
			BindingResult bindingResult=((BindException)runtimeException).getBindingResult();
			response=new Response(bindingResult);
		}else {
			response.setMessage(runtimeException.getClass().getSimpleName() + ":" + runtimeException.getMessage());
			//response.setExceptionType(validException.getClass().getSimpleName() + ":" + validException.getMessage());
		}
		return response;
	}

	/**
	 * 自定义异常处理
	 * @param customException
	 * @return
	 */
	@ExceptionHandler(CustomException.class)
	@ResponseBody
	public Response customExceptionHandler(CustomException customException) {
		StringWriter sw=new StringWriter();
		customException.printStackTrace(new PrintWriter(sw,true));
		logger.error("自定义异常："+sw.toString());//记录详细错误日志
//		customException.printStackTrace();//打印详细错误日志
		Response response = new Response();
		response=new Response(customException);
		//response.setExceptionType(validException.getClass().getSimpleName() + ":" + validException.getMessage());
		return response;
	}

	/**
	 * JSR 303 验证失败异常
	 * @param validException
	 * @return
     */
	@ExceptionHandler(MethodArgumentNotValidException.class)
	@ResponseBody
	public Response validExceptionHandler(MethodArgumentNotValidException validException) {
		StringWriter sw=new StringWriter();
		validException.printStackTrace(new PrintWriter(sw,true));
		logger.error("JSR303验证异常："+sw.toString());//记录详细错误日志
		validException.printStackTrace();//打印详细错误日志
		BindingResult bindingResult= validException.getBindingResult();
		Response response=new Response(bindingResult);
		return response;
	}




	/**
	 * @Description: 判断是否是ajax请求 @author @param: @param
	 * request @param: @return @return: boolean @throws
	 */
	protected boolean isAjaxCall(HttpServletRequest request) {
		return ("XMLHttpRequest".equals(request.getHeader("X-Requested-With")));
	}


}
